package com.example.treehole.utils;

import android.content.Context;

import com.example.treehole.api.Api;
import com.example.treehole.api.ApiConfig;
import com.example.treehole.api.Callback;
import com.example.treehole.entity.CommentResponse;
import com.example.treehole.entity.DynamicResponse;
import com.example.treehole.entity.UserEntity;
import com.example.treehole.entity.UserResponse;
import com.example.treehole.viewobject.DynamicComment;
import com.example.treehole.viewobject.DynamicOutSide;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ConvertEntityUtils {

    public static List<DynamicOutSide> convertToDynamicOutSide(List<DynamicResponse.DataDTO> list){
        List<DynamicOutSide> res=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            DynamicOutSide entity=new DynamicOutSide();
            try{
                entity.setId(list.get(i).getDynamicEntity().getId());
                entity.setIvAuthorHeader(list.get(i).getDynamicEntity().getUser().getHeadUrl());
                entity.setTvAuthor(list.get(i).getDynamicEntity().getUser().getName());
                entity.setTvContent(list.get(i).getDynamicEntity().getText());
                entity.setTvTitle(list.get(i).getDynamicEntity().getTitle());
                entity.setScore(list.get(i).getDynamicEntity().getMark());
                entity.setTvTime(DateUtils.timeTip(list.get(i).getDynamicEntity().getModifyTime()));
                if (list.get(i).getImagesUrl()==null){
                    entity.setType(1);
                }else{
                    entity.setType(2);
                    //有图默认取第一张当封面
                    entity.setIvContent(list.get(i).getImagesUrl().get(0));
                }
                res.add(entity);
            }catch (Exception e){
                e.printStackTrace();
            }

        }
        return res;
    }

    /**
    * date: 2022/4/27 13:42
    * @author: zzs
    * Description: 下面是我的树洞，相于上面减少了type，用户头像，昵称，添加了点赞数等
     * 这里重新写，另外适配器也重写
    */
    public static List<DynamicOutSide> convertToDynamicOutSideForOnly(List<DynamicResponse.DataDTO> list){
        List<DynamicOutSide> res=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            DynamicOutSide entity=new DynamicOutSide();
            try {
                entity.setId(list.get(i).getDynamicEntity().getId());
                entity.setTvContent(list.get(i).getDynamicEntity().getText());
                entity.setTvTitle(list.get(i).getDynamicEntity().getTitle());
                entity.setScore(list.get(i).getDynamicEntity().getMark());
                entity.setTvTime(DateUtils.timeTip(list.get(i).getDynamicEntity().getModifyTime()));
                if (list.get(i).getImagesUrl()!=null) {
                    //有图默认取第一张当封面
                    entity.setIvContent(list.get(i).getImagesUrl().get(0));
                }
                entity.setTvVisits(list.get(i).getDynamicEntity().getVisits());
                entity.setTvStars(list.get(i).getDynamicEntity().getStar());
                entity.setTvComments(list.get(i).getDynamicEntity().getCommentsNumber());
                res.add(entity);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return res;
    }

    /**
    * date: 2022/4/28 19:43
    * @author: zzs
    * Description: 服务接口传回的评论数据没有带用户头像和昵称，这里重新定义实体进行获取并转换然后再传到适配器
     *      private String userId;
     *     private String userName;
     *     private String userHeader;
     *     private String publishId;
     *     private String comment;
     *     private String tvTime;
    */
    public static List<DynamicComment> convertToDynamicComment(Context context,List<CommentResponse.DataDTO> list){
        List<DynamicComment> res=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            DynamicComment entity=new DynamicComment();
            entity.setUserId(list.get(i).getUserId());
            //获取头像和昵称
            String url=ApiConfig.GET_USER+"/"+entity.getUserId();
            String str = Api.config(url,new HashMap<>()).getRequestSync(context);
            UserResponse userResponse=new Gson().fromJson(str,UserResponse.class);
            UserEntity userEntity=userResponse.getUserEntity();
            entity.setUserName(userEntity.getName());
            entity.setUserHeader(userEntity.getHeadUrl());

            entity.setPublishId(list.get(i).getPublishId());
            entity.setComment(list.get(i).getText());
            entity.setTvTime(DateUtils.timeTip(list.get(i).getCreateTime()));
            res.add(entity);
        }
        return res;
    }

}
